<?php
	//SANITIZE ALL ESCAPES
	$sanitize_all_escapes=true;

	//STOP FAKE REGISTER GLOBALS
	$fake_register_globals=false;

	include_once("../../globals.php");
	include_once("$srcdir/lists.inc");
	include_once("$srcdir/acl.inc");
	include_once("$srcdir/options.inc.php");
	include_once("$srcdir/formdata.inc.php");

	$thisauth = acl_check('patients', 'med');
	if ($thisauth) {
		$tmp = getPatientData($pid, "squad");
		if ($tmp['squad'] && ! acl_check('squads', $tmp['squad']))
			$thisauth = 0;
	}
	if (!$thisauth) {
		echo "<p>(".htmlspecialchars(xl('Issues not authorized'),ENT_NOQUOTES).")</p>\n";
		echo "</body>\n</html>\n";
		exit();
	}

	$numcols = '1';
	$erx_upload_complete = 0;
	$old_key="";
	$display_current_medications_below=1;
	foreach ($ISSUE_TYPES as $key => $arr) {
		$query = "SELECT * FROM lists WHERE pid = ? AND type = ? AND ";
		$query .= "(enddate is null or enddate = '' or enddate = '0000-00-00') ";
		if($GLOBALS['erx_enable'] && $GLOBALS['erx_medication_display'] && $key=='medication')
			$query .= "and erx_uploaded != '1' ";
		if($GLOBALS['erx_enable'] && $GLOBALS['erx_allergy_display'] && $key=='allergy')
			$query .= "and erx_uploaded != '1' ";
		$query .= "ORDER BY begdate";
		$pres = sqlStatement($query, array($pid, $key) );
		
		if (sqlNumRows($pres) > 0 || $arr[4] == 1) {
				$old_key=$key;
				echo '<div id="'. $key. '" class="patient_secondary">';
					echo "<h2>222".htmlspecialchars($arr[0],ENT_NOQUOTES)."<span style='float: right; background-color: rgb(228, 228, 228); border-radius: 5px 5px 5px 5px; border-right: 1px solid rgb(204, 204, 204); border-width: 2px 1px 1px; border-style: solid; border-color: -moz-use-text-color rgb(204, 204, 204) rgb(204, 204, 204); -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none; padding: 0px 10px;'> ".sqlNumRows($pres)." </span>";
					echo "<a class='addIcon iframe_stats css_button_left' title='".htmlspecialchars(xl('Manage'),ENT_NOQUOTES)."' href='../patient_file/summary/add_edit_issue.php?issue=0&thistype=medical_problem'>";
					echo '<i class="icon-plus-sign"></i>';
					echo "</a></h2></div>";
				
			if (sqlNumRows($pres) == 0) {
			  if ( getListTouch($pid,$key) ) {
				// Data entry has happened to this type, so can display an explicit None.
				//echo "  <tr><td colspan='$numcols' class='text'>&nbsp;&nbsp;" . htmlspecialchars( xl('None'), ENT_NOQUOTES) . "</td></tr>\n";
			   // echo "  <li><p>" . htmlspecialchars( xl('None'), ENT_NOQUOTES) . "</p></li>";
			  }
			  else {
				// Data entry has not happened to this type, so show 'Nothing Recorded"
				//echo "  <li><p>" . htmlspecialchars( xl('Nothing Recorded'), ENT_NOQUOTES) . "</p></li>";
			   // echo "  <li><p>No se han ingresado datos.</p></li>";
			  }
			}else{
				echo "<ul>";
				while ($row = sqlFetchArray($pres)) {
					// output each issue for the $ISSUE_TYPE
					if (!$row['enddate'] && !$row['returndate'])
						$rowclass="noend_noreturn";
					else if (!$row['enddate'] && $row['returndate'])
						$rowclass="noend";
					else if ($row['enddate'] && !$row['returndate'])
						$rowclass = "noreturn";

					echo " <li>";

					//turn allergies red and bold and show the reaction (if exist)
					if ($key == "allergy") {
							$reaction = "";
							if (!empty($row['reaction'])) {
								$reaction = " (" . $row['reaction'] . ")";
							}
							echo "  <p>&nbsp;&nbsp;" . htmlspecialchars( $row['title'] . $reaction, ENT_NOQUOTES) . "</p>";
					}
					else {
						echo "  <p>&nbsp;&nbsp;" . htmlspecialchars($row['title'],ENT_NOQUOTES) . "</p>";
					}
					echo " </li>";
				}
				echo "</ul>";
			}    		
		}
	}
	?>